﻿using Sustainalytics.ESG.Entities;
using System;

namespace Sustainalytics.ScreeningTool.Synchronization
{
    public static class FloatExtensions
    {
        public static int ToInt(this double x)
            => (int)x;

        public static double Round(this double value, int decimals = 0)
            => Math.Round(value, decimals, MidpointRounding.AwayFromZero);

        public static double Round(this float value, int decimals = 0)
            => Round((double)value, decimals);

        public static double? Round(this double? value, int decimals = 0)
            => value?.Round(decimals);

        public static double? Round(this float? value, int decimals = 0)
            => value?.Round(decimals);

        public static double PercentileRound(this double value, int decimals = 0)
            => Round(value * 100, decimals);

        public static Timestamped<double> Round(this Timestamped<double> timestamped, int decimals = 0)
            => timestamped?.Bind<double>(x => Round(x, decimals));

        public static Timestamped<double> Round(this TimestampedFloat timestamped, int decimals = 0)
            => timestamped?.Bind<double>(x => Round(x, decimals));

        public static Timestamped<double?> Round(this Timestamped<double?> timestamped, int decimals = 0)
            => timestamped?.Bind<double?>(x => Round(x, decimals));
    }
}
